﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Net;
using System;

namespace CNdotnet.Ezsocio.ContactService.Email {
    public class Email163VipService : IEmailService {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public string EmailSystem {
            get { return "vip.163.com"; }
        }

        public string StartUrl {
            get { return "https://ssl1.vip.163.com/logon.m"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("gb2312"); }
        }

        #endregion

        #region Events

        #endregion

        public MailContractCollection GetContracts(string userName, string pwd) {           
            var nvc = new NameValueCollection();
            nvc.Add("username", userName);
            nvc.Add("password", pwd);
            nvc.Add("prestyle", "&nbsp;&nbsp;&nbsp;默　　认");
            nvc.Add("secure", "");
            nvc.Add("enterVip", "true");
            nvc.Add("verifycookie", "1");
            nvc.Add("language", "0");
            nvc.Add("style", "-1");
            nvc.Add("login", "登 录");
            string postContent = WebOpHelper.GetNameValueRowData(nvc, '&');
            var cookies = new NameValueCollection();
            CookieContainer cc = new CookieContainer();
            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "http://vip.163.com/";
            string contentType = "application/x-www-form-urlencoded";
            string returnUrl = StartUrl;
            string html = WebOpHelper.Post(StartUrl, accept, contentType, refer, language, postContent, cc, DefaultEncoding, out returnUrl);
            string nextUrl = WebOpHelper.Resove(html, "<META HTTP-EQUIV=REFRESH CONTENT=\"0;URL=", "\">");
            html = WebOpHelper.GetHtml(nextUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            string sid = WebOpHelper.GetQ(new Uri(returnUrl), "sid");
            Uri u = new Uri(returnUrl);
            string url = u.Scheme + "://" + u.Host + "/a/s?sid="+sid+"&func=global:sequential";
            postContent = "<?xml version=\"1.0\"?><object><array name=\"items\"><object><string name=\"func\">pab:searchContacts</string><object name=\"var\"><array name=\"order\"><object><string name=\"field\">FN</string><boolean name=\"ignoreCase\">true</boolean></object></array></object></object><object><string name=\"func\">user:getSignatures</string></object><object><string name=\"func\">pab:getAllGroups</string></object></array></object>";
                      
            cookies.Add("mail_sid", sid);
            cookies.Add("mail_uid", userName + "@vip.163.com");
            cc.SetCookies(new Uri(url), WebOpHelper.GetNameValueRowData(cookies, ','));
            string cks = cc.GetCookieHeader(new Uri(url)); 
            html = WebOpHelper.Post(url, accept, "application/xml", refer, language, postContent, cc, Encoding.GetEncoding("utf-8"), out returnUrl);

            return WangyiResover.Resove(html);
        }
    }
}
